﻿/*****************************************************
 * 本类库的核心系 JNTemplate
 * (C) JNTemplate 2011
 * 作者：翅膀的初衷 QQ:4585839
 * 网址：http://www.JiNianNet.com
 *****************************************************/

using System;
using System.Text;
using System.Collections.Generic;
using JinianNet.JNTemplate.Process;
using JinianNet.JNTemplate.Process.Element;

namespace JinianNet.JNTemplate
{
    public class Engine : IEngine
    {

        #region IEngine 成员

        private Process.VariableScope variableScope;
        public Process.VariableScope VariableScope
        {
            get { return variableScope; }
            set { variableScope = value; }
        }

        private string _currentPath;
        public string CurrentPath
        {
            get { return _currentPath; }
            set { _currentPath = value; }
        }
        private Encoding _charset;
        public Encoding Charset
        {
            get { return _charset; }
            set { _charset = value; }
        }


        public Engine()
        {
            System.Web.HttpContext ctx = System.Web.HttpContext.Current;
            if (ctx == null || ctx.CurrentHandler == null)
            {
                _charset = Encoding.UTF8;
                _currentPath = System.Web.HttpRuntime.AppDomainAppPath;
            }
            else
            {
                _charset = ctx.Request.ContentEncoding;
                _currentPath = ctx.Request.PhysicalApplicationPath;
            }

            variableScope = new VariableScope();
        }

        public Engine(string path, Encoding charset)
        {
            CurrentPath = path;
            Charset = charset;
            VariableScope = new VariableScope();
        }

        public ITemplate CreateTemplate()
        {
            TemplateContext context =new TemplateContext();
            context.Charset = Charset;
            context.CurrentPath = CurrentPath;
            context.TempData = new VariableScope( variableScope);
            Template template = new Template();
            template.Context = context;
            return template;
        }

        #endregion
    }
}
